<?php
eval('
function xoops_module_install_' . $module_dirname . '(&$module)
{
	return xigg_xoops_module_install("' . $module_dirname . '", $module);
}
');

if (!function_exists('xigg_xoops_module_install')) {
    function xigg_xoops_module_install($module_dirname, &$module)
    {
        require dirname(__FILE__) . '/common.php';
        $installer =& new xigg_xoops_module_installer($xigg);
        if (defined('XOOPS_CUBE_LEGACY')) {
            $root =& XCube_Root::getSingleton();
		    $root->mDelegateManager->add('Legacy.Admin.Event.ModuleInstall.' . ucfirst($module_dirname) . '.Success', array(&$installer, 'execute'));
		    return true;
        } else {
            require_once 'SabaiXOOPS/XOOPS2ModuleInstallLog.php';
            return $installer->execute($module, new SabaiXOOPS_XOOPS2ModuleInstallLog());
        }
    }

    class xigg_xoops_module_installer
    {
        var $_app;

        function xigg_xoops_module_installer(&$app)
        {
            $this->_app =& $app;
        }

        function execute(&$module, &$log)
        {
            // init cache directories
            $log->add('Initializing cache directories...');
            $cache_root = $this->_app->getCacheDir();
            foreach (array('HTMLPurifier/HTML', 'HTMLPurifier/CSS', 'HTMLPurifier/URI', 'Cache_Lite') as $dir) {
                if (!is_writable($cache_dir = $cache_root . '/' . $dir)) {
                    if (!@chmod($cache_dir, 0777)) {
                        $log->addWarning(sprintf('Failed setting permission of cache directory %s to 0777. Please manually set the permission of this directory to 0777.', $cache_dir));
                    } else {
                        $log->add(sprintf('Setting permission of cache directory %s to 0777', $cache_dir));
                    }
                } else {
                    $log->add(sprintf('Cache directory %s is writable.', $cache_dir));
                }
            }

            // create database tables
            $log->add('Initializing models...');
            $model =& $this->_app->getService('Model');
            $has_error = false;
            $schema_created = array();
            foreach (array('Access', 'Category', 'Comment', 'Node', 'Node2tag', 'Plugin', 'Tag', 'Trackback', 'Vote') as $entity_name) {
                if (in_array($entity_name, $schema_created)) {
                    continue;
                }
                if (false === $schema =& $model->getSchema($entity_name)) {
                    $log->addWarning(sprintf('Failed loading schema file for %s', $f_entity_name));
                    $has_error = true;
                } else {
                    // make sure all tables for foreign schema are initialized first
                    foreach ($schema->getForeignSchemaNames() as $f_entity_name) {
                        if (in_array($f_entity_name, $schema_created)) {
                            continue;
                        }
                        if (false === $f_schema =& $model->getSchema($f_entity_name)) {
                            $log->addWarning(sprintf('Failed loading schema file for %s', $f_entity_name));
                            $has_error = true;
                            break;
                        }
                        $schema_created[] = $f_entity_name;
                        if (!$f_schema->create()) {
                            $log->addWarning(sprintf('Failed creating database tables for %s.', $f_entity_name));
                            $has_error = true;
                            break;
                        } else {
                            $log->add(sprintf('Database tables for %s created.', $f_entity_name));
                        }
                    }
                }

                if (!$has_error) {
                    $schema_created[] = $entity_name;
                    if (!$schema->create()) {
                        $log->addWarning(sprintf('Failed creating database tables for %s.', $entity_name));
                        $has_error = true;
                    } else {
                        $log->add(sprintf('Database tables for %s created.', $entity_name));
                    }
                }

                if ($has_error) {
                    foreach (array_reverse($schema_created) as $_entity_name) {
                        $schema =& $model->getSchema($_entity_name);
                        $schema->drop();
                        $log->add(sprintf('Database tables for %s dropped.', $_entity_name));
                    }
                    break;
                }
            }
            return !$has_error;
        }
    }
}